package com.atguigu.day06.test;

import java.util.Arrays;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/5 9:35
 * @注释
 */
class BubbleSort1{
    public static void main(String[] args) {
        int[] arr = {70, 90, 50, 40, 60, 20, 80};
        System.out.println("排序前: " + Arrays.toString(arr));
        //排序
        //优化
        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag = true;//假设数组有序,输出true
            for (int j = 0; j < arr.length - 1; j++) {
                //数组无序进入判断
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = false;
                }

            }
            //如果进入了排序操作,则数组无序,再次执行,否则执行break跳出循环
            if (flag) {
                break;
            }
            System.out.println("排序中 -------->" + Arrays.toString(arr));
        }
        //排序后
        System.out.println("排序后: " + Arrays.toString(arr));
    }
}
public class BubbleSort {
    public static void main(String[] args) {
        //排序前
        int[] arr = {70, 60, 50, 40, 30, 20, 10};
        System.out.println("排序前: " + Arrays.toString(arr));
        //排序
        //最基本的一种
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        //排序后
        System.out.println("排序后: " + Arrays.toString(arr));

    }
}
